'use strict';

const Controller = require('egg').Controller;

class SysBranchController extends Controller {

    /**
     * 添加单位
     */
    async addBranch() {
        const {
            ctx,
            service,
        } = this;
        const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
        if (!user_id) {
            ctx.response.failByCode('0003');
            return;
        }
        const branchparams = ctx.request.body;
        const branchModel = {};
        branchModel.branch_type = branchparams.branchType || '';
        if (branchparams.name) {
            branchModel.name = branchparams.name;
        }
        if (!branchparams.areaCode) {
            ctx.response.failByCode('0001', '', '请选择区域');
            return;
        }
        branchModel.area_id = branchparams.areaCode;
        if (!branchparams.code) {
            ctx.response.failByCode('0001', '', '缺少参数code');
            return;
        }
        branchModel.code = branchparams.code;
        if (branchparams.parent_code) {
            branchModel.parent_code = branchparams.parent_code;
        }
        if (branchparams.phone) {
            branchModel.phone = branchparams.phone;
        }
        if (branchparams.remarks) {
            branchModel.remarks = branchparams.remarks;
        }
        const branchInfo = await service.admin.sysBranch.findBranchByCode(branchparams.code);
        if (branchInfo.length > 0) {
            ctx.response.failByCode('1001', '', '该分支机构代码已存在');
            return;
        }
        branchModel.type = parseInt(branchparams.type || 0);
        branchModel.create_time = ctx.getDate_YYYYMMDDHHmmss();
        branchModel.update_time = ctx.getDate_YYYYMMDDHHmmss();
        branchModel.creator_id = user_id;
        await service.admin.sysBranch.addBranch(branchModel);
        ctx.response.success();
        // 添加操作日志
        const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】添加了机构【${JSON.stringify(branchModel)}】`;
        ctx.writeSysOperationLogs('添加机构', ctx.getSystemLogType('CREATE'), { curAdminUser: ctx.curAdminUser, des });
    }
    /**
     * 查询所有单位机构，区分返回数据格式,默认是按树的形式返回
     * @RequestBody { String } format 'tree' 树 | 'list' 列表
     */
    async findAllBranch() {
        const TREE = 'tree';
        const LIST = 'list';
        const { ctx, service } = this;
        const request = ctx.request.body;
        const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
        if (!user_id) {
            ctx.response.failByCode('0003');
            return;
        }
        let result;
        const format = request.format;
        // 未传值 || 字符串不符合
        if (typeof format !== 'string' || (format !== TREE && format !== LIST)) {
            result = await service.admin.sysBranch.queryLeafBranch(request);
        } else if (format == LIST) {
            result = await service.admin.sysBranch.queryLeafBranch(request);
        } else {
            result = await service.admin.sysBranch.querySysBranch(request);
        }
        ctx.response.success(result);
        // 添加操作日志
        const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】查询了机构列表`;
        ctx.writeSysOperationLogs('查询所有单位机构', ctx.getSystemLogType('QUERY'), { curAdminUser: ctx.curAdminUser, des });
    }
    /**
     * 删除单位
     */
    async delBranch() {
        const { ctx, service } = this;
        const user_id = ctx.curAdminUser.id;
        if (!user_id) {
            ctx.response.failByCode('0001');
            return;
        }
        const code = ctx.request.body.code;
        const allBraches = await service.admin.sysBranch.queryAllBranch();
        const paretCodes = allBraches.length > 0 && allBraches.map(obj => obj.parent_code) || [];
        if (paretAreas.includes(code)) {
           ctx.response.failByCode('0001');
           return;
        }
        const result =  await service.admin.sysBranch.findBranchByCodeOfPetRegister(code);
        if (result.length > 0) {
         ctx.response.failByCode('0001');
         return;
        }

        await service.admin.sysBranch.delBranch(code);
        ctx.response.success();
        const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】删除的机构代码为【${code}】`;
        ctx.writeSysOperationLogs('删除单位', ctx.getSystemLogType('DELETE'), { curAdminUser: ctx.curAdminUser, des });
    }
    /**
     * 更新单位
     */
    async updateBranch() {
        const { ctx, service } = this;
        const user_id = ctx.curAdminUser.id;
        if (!user_id) {
            ctx.response.failByCode('0003');
            return;
        }
        const branchParams = ctx.request.body;
        const brachModel = {};
        if (!branchParams.code) {
            ctx.response.failByCode('0001');
            return;
        }
        if (branchParams.phone) {
            brachModel.phone = branchParams.phone;
        }
        if (branchParams.name) {
            brachModel.name = branchParams.name;
        }
        if (branchParams.remarks) {
            brachModel.remarks = branchParams.remarks;
        }
        brachModel.update_time = new Date();
        await service.admin.sysBranch.updateBranch(branchParams.code, brachModel);
        ctx.response.success();
        // 添加操作日志
        const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】更改的机构代码为【${branchParams.code}】`;
        ctx.writeSysOperationLogs('更新单位', ctx.getSystemLogType('UPDATE'), { curAdminUser: ctx.curAdminUser, des });
    }
    /**
     * 根据code查询单位
     */
    async findBranchByCode() {
        const {
            ctx,
            service,
        } = this;
        const user_id = ctx.curAdminUser.id;
        if (!user_id) {
            ctx.response.failByCode('0003');
            return;
        }
        const branchParams = ctx.request.body;
        if (!branchParams.code) {
            ctx.response.failByCode('0001');
            return;
        }
        const result = await service.admin.sysBranch.findBranchByCode(branchParams.code);
        ctx.response.success(result);
        const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】查询的机构代码为【${result}】`;
        ctx.writeSysOperationLogs('根据code查询单位', ctx.getSystemLogType('QUERY'), { curAdminUser: ctx.curAdminUser, des });
    }
}
module.exports = SysBranchController;
